package com.yh.security;

import com.yh.service.impl.SysPermissionServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author yh
 * @author 2021-1-2
 */
@Service
@AllArgsConstructor
public class MyUserDetailsService implements UserDetailsService {
    private final SysPermissionServiceImpl permissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUserDTO sysUserDTO = permissionService.userPermissions(username);
        if(StringUtils.isEmpty(sysUserDTO)){
            return null;
        }
        return new User(sysUserDTO.getUsername(),sysUserDTO.getPassword(),sysUserDTO.getAuthorities());
    }
}
